//
//  LookJewelryDetailViewController.m
//  MMLOOK
//
//  Created by dllo on 15/8/27.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "LookJewelryDetailViewController.h"
#import "AsynConnect.h"
#import "HomeCollectionViewCell.h"
#import "LookjeweDetailViewController.h"
#import "SaveTools.h"
#import "MBProgressHUD.h"
#import "JudgeConnection.h"
#import "Alert.h"

@interface LookJewelryDetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)UICollectionView *collection;
@property (nonatomic, retain)NSMutableArray *jeweArray;
@property (nonatomic, copy) NSString *url;
@property (nonatomic, retain) MBProgressHUD *mbProView;

@end

@implementation LookJewelryDetailViewController

- (void)dealloc
{
    [_url release];
    [_mbProView release];
    [_jeweArray release];
    [_collection release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.url = @"http://api.liwushuo.com/v1/channels/116/items?limit=20&offset=0";
        self.jeweArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.translucent = NO;
    self.title = @"小配饰";
    [self CreatCollection];
    [self readLocalData];
    [self Connection];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)CreatCollection
{
    UICollectionViewFlowLayout *layout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.width * 328 / 720);
    layout.minimumInteritemSpacing = 15 * OffHeight;
    layout.sectionInset = UIEdgeInsetsMake(15 * OffHeight, 15 * OffWidth, 15 * OffHeight, 15 * OffWidth);
    
    self.collection = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 104) collectionViewLayout:layout] autorelease];
    _collection.dataSource = self;
    _collection.delegate = self;
    _collection.backgroundColor = [UIColor whiteColor];
    [_collection registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:@"jewe"];
    [self.view addSubview:_collection];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_jeweArray.count == 0) {
        return 0;
    }
    return _jeweArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"jewe" forIndexPath:indexPath];
    NSMutableDictionary *dic = [_jeweArray objectAtIndex:indexPath.row];
    
    [cell sendDic:dic];
    return cell;
}

- (void)Connection
{
    [self createMbProView];
    
    [AsynConnect asynConnectWithUrlstr:_url parmater:nil block:^(NSData *data) {
        
        if (data) {
            BOOL saveResult = [SaveTools saveDataWithName:[_url hash] directory:NSLibraryDirectory data:data];// 有网络时 数据储存在本地
            NSLog(@"小配饰存储: %d", saveResult);
            
            [self.jeweArray removeAllObjects];
            
            NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSMutableDictionary *littleDic = [dic objectForKey:@"data"];
            NSMutableArray *arr = [littleDic objectForKey:@"items"];
            
            [_jeweArray addObjectsFromArray:arr];
            [_collection reloadData];
            [_mbProView hide:YES];
        }
    }];
}

- (void)readLocalData
{
    NSData *data = [SaveTools readDataWithName:[_url hash] directory:NSLibraryDirectory];
    if (data) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *littleDic = [dic objectForKey:@"data"];
        NSMutableArray *arr = [littleDic objectForKey:@"items"];
        [_jeweArray addObjectsFromArray:arr];
        
        [_collection reloadData];
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([JudgeConnection judgeConnection]) {
        
        LookjeweDetailViewController *lookDetail = [[[LookjeweDetailViewController alloc] init] autorelease];
        
        lookDetail.lookJeweDic = [_jeweArray objectAtIndex:indexPath.item];
        
        [self.navigationController pushViewController:lookDetail animated:YES];
    } else {
        
        Alert *alert = [[Alert alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 250) / 2, self.view.frame.size.height * 3 / 4, 250, 50) LabelText:@"当前无网络连接，请检查网络状态"];
        [self.view addSubview:alert];
        [alert release];
    }
}

- (void)createMbProView
{
    self.mbProView = [[[MBProgressHUD alloc] initWithView:self.view] autorelease];
    _mbProView.mode = MBProgressHUDModeIndeterminate;// 样式 菊花
    _mbProView.labelText = @"加载中. . .";
    [_mbProView show:YES];
    [self.view addSubview:_mbProView];
    [_mbProView hide:YES afterDelay:20];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
